package com.mmp.main.servlet.device;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDevicePpmDetail;
import com.mmp.main.util.Default;

public class PPMVisitDisplayServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String deviceId = request.getParameter("deviceId");
		String visitMonth = request.getParameter("visitMonth");
		String visitYear = request.getParameter("visitYear");

		HospitalDevice device = new HospitalDevice();
		if (deviceId != null)
			device = dbm.findById(Integer.parseInt(deviceId),
					HospitalDevice.class);		


		if (visitMonth != null && visitYear != null) {

			HospitalDevicePpmDetail ppmVisit = new HospitalDevicePpmDetail();

			ppmVisit.setHospitalDevice(device);
			ppmVisit.setVisitMonth(Integer.parseInt(visitMonth));
			ppmVisit.setVisitYear(Integer.parseInt(visitYear));

			List<HospitalDevicePpmDetail> searchResults = dbm.find(ppmVisit);
			if (searchResults != null && searchResults.size() == 1) {
				request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, searchResults.get(0).getHospitalDevice());
				request.setAttribute("ppmVisit", searchResults.get(0));
			} else
				request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, dbm.find(device).get(0));

			basicForward(request, response, "device/PPMVisit.jsp");
			return;
		}

		// if (request.getAttribute(Default.DEVICE_INFO_ATTR_NAME) == null)
		// request.setAttribute(Default.DEVICE_INFO_ATTR_NAME,
		// dbm.find(device).get(0));

		// dbm.destory();

		forward(request, response, "device/PPMVisit.jsp");
	}
}
